package com.warehouse.service.impl;

import com.warehouse.dto.ProductDTO;
import com.warehouse.entity.Product;
import com.warehouse.mapper.ProductMapper;
import com.warehouse.service.ProductService;
import com.warehouse.util.BoxQuantityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> getAllProducts() {
        return productMapper.selectAll();
    }

    @Override
    public List<ProductDTO> getAllProductsWithBoxDisplay() {
        List<Product> products = productMapper.selectAll();
        List<ProductDTO> productDTOs = new ArrayList<>();
        
        for (Product product : products) {
            ProductDTO dto = new ProductDTO();
            dto.setId(product.getId());
            dto.setProductName(product.getProductName());
            dto.setSpecification(product.getSpecification());
            dto.setCategory(product.getCategory());
            dto.setUnit(product.getUnit());
            dto.setPrice(product.getPrice());
            dto.setStockQuantity(product.getStockQuantity());
            dto.setMinStock(product.getMinStock());
            dto.setBoxQuantity(product.getBoxQuantity());
            dto.setBoxUnit(product.getBoxUnit());
            dto.setCreatedTime(product.getCreatedTime());
            dto.setUpdatedTime(product.getUpdatedTime());
            
            // 计算箱数量显示
            String boxDisplay = BoxQuantityUtil.formatBoxQuantity(
                product.getStockQuantity(), 
                product.getBoxQuantity(), 
                product.getBoxUnit(), 
                product.getUnit()
            );
            
            // 将箱数量显示添加到DTO中（通过反射或其他方式）
            // 这里我们可以在前端处理，或者添加一个额外的字段
            productDTOs.add(dto);
        }
        
        return productDTOs;
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> getProductsByCategory(String category) {
        return productMapper.selectByCategory(category);
    }

    @Override
    public List<Product> getProductsByName(String productName) {
        return productMapper.selectByProductName(productName);
    }

    @Override
    public List<Product> getLowStockProducts() {
        return productMapper.selectLowStock();
    }

    @Override
    @Transactional
    public Product createProduct(Product product) {
        productMapper.insert(product);
        return product;
    }

    @Override
    @Transactional
    public Product updateProduct(Product product) {
        productMapper.update(product);
        return product;
    }

    @Override
    @Transactional
    public boolean updateStock(Long productId, Integer quantity) {
        return productMapper.updateStock(productId, quantity) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProduct(Long id) {
        return productMapper.deleteById(id) > 0;
    }
    
    @Override
    public long getProductCount() {
        return productMapper.countAll();
    }
} 